﻿using PTop.Domain.Entity.Article;
using PTop.Domain.IRepository.Article;
using PTop.Repository.Article;
using System.Collections.Generic;
using System.Linq;

namespace PTop.Application.Article
{
    /// <summary>
    /// ArticleTagRelationApp
    /// </summary>	
    public class ArticleTagRelationApp
    {
        private IArticleTagRelationRepository service = new ArticleTagRelationRepository();

        public List<ArticleTagRelationEntity> GetList()
        {
            return service.IQueryable().ToList();
        }

        public ArticleTagRelationEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void Delete(ArticleTagRelationEntity entity)
        {
            service.Delete(entity);
        }

        public void SubmitForm(ArticleTagRelationEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                service.Update(entity);
            }
            else
            {
                entity.Create();
                service.Insert(entity);
            }
        }

        /// <summary>
        /// 跟新文章标签
        /// </summary>
        /// <param name="articleId"></param>
        /// <param name="tagIds"></param>
        public void UpdateRelation(string articleId, string[] tagIds)
        {
            service.Delete(m => m.F_ArticleId == articleId);
            var list = new List<ArticleTagRelationEntity>();
            if (tagIds.Length > 0)
            {
                foreach (var tagId in tagIds)
                {
                    var r = new ArticleTagRelationEntity
                    {
                        F_ArticleId = articleId,
                        F_TagId = tagId
                    };
                    r.Create();
                    list.Add(r);
                }
                service.Insert(list);
            }
        }
    }
}